package com.violence.web.controller;

import com.violence.web.service.BaseElasticService;
import com.violence.web.utils.EsUtils;
import com.violence.web.utils.MapUtils;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/api/es")
public class TestEsController {

    @Autowired
    RestHighLevelClient restHighLevelClient;

    @Autowired
    private BaseElasticService baseElasticService;

    /**
     * 创建索引
     *
     * @param index
     */
//    @GetMapping("/createIndex")
//    public Map createIndex(String index) {
//        EsUtils.createMapping(restHighLevelClient, index);
//        return MapUtils.create("no", 1, "msg", "创建成功");
//    }


    @GetMapping("/createIndex")
    public Map createIndex(String index) {
        String sql = "{\n" +
                "    \"properties\": {\n" +
                "      \"id\":{\n" +
                "        \"type\":\"integer\"\n" +
                "      },\n" +
                "      \"userId\":{\n" +
                "        \"type\":\"integer\"\n" +
                "      }\n" +
                "    }\n" +
                "  }";
        baseElasticService.createIndex(index, sql);
        return MapUtils.create("no", 1, "msg", "成功");
    }

//    @GetMapping("/createIndexJoin")
//    public Map createIndexJoin(String index) {
//        String sql = "{\n" +
//                "    \"properties\": {\n" +
//                "      \"id\":{\n" +
//                "        \"type\":\"integer\"\n" +
//                "      },\n" +
//                "      \"userId\":{\n" +
//                "        \"type\":\"integer\"\n" +
//                "      }\n" +
//                "    }\n" +
//                "  }";
//        baseElasticService.createIndex(index, sql);
//        return MapUtils.create("no", 1, "msg", "成功");
//    }


//    /**
//     * 创建地址mapping
//     *
//     * @param index
//     * @return
//     */
//    @GetMapping("/createIndexLocation")
//    public Map createIndexLocation(String index) {
//        baseElasticService.createIndexLocation(index);
//        return MapUtils.create("no", 1, "msg", "成功");
//    }

    /**
     * 删除索引
     *
     * @param index
     * @return
     */
    @GetMapping("/deleteIndex")
    public Map deleteIndex(String index) {
        baseElasticService.deleteIndex(index);
        return MapUtils.create("no", 1, "msg", "成功");
    }

    /**
     * 新增数据
     *
     * @param index
     * @return
     */
    @GetMapping("/insert")
    public Map insert(String index) {
       // baseElasticService.insert(index);
        return MapUtils.create("no", 1, "msg", "成功");
    }

    /**
     * 批量保存
     * @param index
     * @return
     */
    @GetMapping("/batchInsert")
    public Map batchInsert(String index) {
        baseElasticService.batchInsert(index);
        return MapUtils.create("no", 1, "msg", "成功");
    }

    /**
     * 删除数据
     *
     * @param index
     * @param dataId
     * @return
     */
    @GetMapping("/delete")
    public Map delete(String index, String dataId) {
        baseElasticService.delete(index, dataId);
        return MapUtils.create("no", 1, "msg", "成功");
    }

    /**
     * 更新
     * @param index
     * @param dataId
     * @return
     */
    @GetMapping("/update")
    public Map update(String index, String dataId) {
        baseElasticService.update(index, dataId);
        return MapUtils.create("no", 1, "msg", "成功");
    }

    /**
     * 查询
     *
     * @param index
     * @param name
     * @return
     */
    @GetMapping("/search")
    public Map search(String index, String name) {
       // baseElasticService.search(index, name);
        return MapUtils.create("no", 1, "msg", "成功");
    }

    /**
     * 快照形式查询
     *
     * @param index
     * @param name
     * @return
     */
    @GetMapping("/scrollSearch")
    public Map scrollSearch(String index, String name) {
      //  baseElasticService.scrollSearch(index, name);
        return MapUtils.create("no", 1, "msg", "成功");
    }

    /**
     * 查询经纬度
     *
     * @param index
     * @return
     */
    @GetMapping("/searchGepPoint")
    public Map searchGepPoint(String index) {
        baseElasticService.searchLocation(index);
        return MapUtils.create("no", 1, "msg", "成功");
    }

}
